// 题目链接：https://leetcode.com/problems/rotate-array/discuss/487529/py3-js-5-different-simple-solutions
/**
 * @param {number[]} nums
 * @param {number} k
 * @return {void} Do not return anything, modify nums in-place instead.
 */
// 方法1: 递归
var rotate1 = function (nums, k) {
  const len = nums.length

  if (k > len) {
    k = k % len
  }

  if (k < 1 || nums.length <= 1) {
    return nums
  }

  nums.unshift(nums.pop())
  k--

  return rotate1(nums, k)
}

// 方法二：遍历
var rotate2 = function (nums, k) {
  const len = nums.length

  if (k > len) {
    k = k % len
  }

  for (let i = 0; i < k; i++) {
    nums.unshift(nums.pop())
  }

  return nums
}

// 方法3
var rotate3 = function (nums, k) {
  const len = nums.length

  if (k > len) {
    k = k % len
  }

  const arr = nums.splice(-k, k)
  nums.unshift(...arr)

  return nums
}

// 方法4
var rotate4 = function (nums, k) {
  const len = nums.length

  if (len < k) {
    k = k % len
  }

  let index = 0

  while (index < k) {
    nums.unshift(nums.pop())
    ++index
  }
}

const nums = [1, 2, 3, 4, 5, 6, 7]
const k = 3

console.log(rotate3(nums, k))

module.exports = {
  rotate1,
  rotate2,
  rotate3,
  rotate4
}
